﻿// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using System.Collections.Generic;
using System.Windows.Media;

using Nova.CodeDOM;

namespace Nova.UI
{
    /// <summary>
    /// The view model for a <see cref="CodeDOM.IfBase"/>.
    /// </summary>
    public class IfBaseVM : BlockStatementVM
    {
        #region /* FIELDS */

        protected ExpressionVM _conditionalVM;
        protected BlockStatementVM _elsePartVM;  // Must be an Else or an ElseIf

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a view model instance for the specified <see cref="CodeDOM.IfBase"/>.
        /// </summary>
        protected IfBaseVM(IfBase ifBase, bool isDescription, Dictionary<CodeObject, CodeObjectVM> dictionary)
            : base(ifBase, isDescription, dictionary)
        {
            _conditionalVM = (ExpressionVM)CreateVM(ifBase.Conditional, isDescription, dictionary);
            _elsePartVM = (BlockStatementVM)CreateVM(ifBase.ElsePart, isDescription, dictionary);
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The underlying <see cref="CodeDOM.IfBase"/> model.
        /// </summary>
        public IfBase IfBase
        {
            get { return (IfBase)CodeObject; }
        }

        /// <summary>
        /// The conditional <see cref="ExpressionVM"/>.
        /// </summary>
        public ExpressionVM ConditionalVM
        {
            get { return _conditionalVM; }
        }

        /// <summary>
        /// The optional <see cref="ElseIfVM"/> or <see cref="ElseVM"/> part.
        /// </summary>
        public BlockStatementVM ElsePartVM
        {
            get { return _elsePartVM; }
        }

        /// <summary>
        /// True if there is an <see cref="ElseIfVM"/> or <see cref="ElseVM"/> part.
        /// </summary>
        public bool HasElseVM
        {
            get { return (_elsePartVM != null); }
        }

        #endregion

        #region /* METHODS */

        #endregion

        #region /* RENDERING */

        public override Brush BorderBrush
        {
            get { return Brushes.PaleTurquoise; }
        }

        public override Brush BackgroundBrush
        {
            get { return Brushes.LightCyan; }
        }

        protected override void RenderArgument(CodeRenderer renderer, RenderFlags flags)
        {
            if (_conditionalVM != null)
                _conditionalVM.Render(renderer, flags);
        }

        protected override void RenderAfter(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderAfter(renderer, flags);
            if (_elsePartVM != null && !flags.HasFlag(RenderFlags.Description))
                _elsePartVM.Render(renderer, flags | RenderFlags.PrefixSpace);
        }

        public override void RenderVisible(CodeRenderer renderer, RenderFlags flags)
        {
            base.RenderVisible(renderer, flags);
            if (_elsePartVM != null)
                _elsePartVM.RenderVisible(renderer, flags | RenderFlags.PrefixSpace);
        }

        public override void UnRender()
        {
            if (_conditionalVM != null)
                _conditionalVM.UnRender();
            if (_elsePartVM != null)
                _elsePartVM.UnRender();
            base.UnRender();
        }

        #endregion
    }
}
